package com.qdlc.p2p.dal.model;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.AccountException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.PropertiesUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.AccountBank;
import com.qdlc.p2p.dal.dto.AccountCash;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 提现Model
 * 
 * @author xx
 * @version 2.0
 * @since 2014年3月17日
 */
public class AccountCashModel extends AccountCash {
	
	/** 当前页码 */
	private int page;
	/** 每页总数 **/
	private int rows;
	/** 用户名 **/
	private String userName;
	/** 真实姓名 **/
	private String realName;
	/** 银行 **/
	private String bankName;
	/** 银行卡编号 **/
	private long bankId;
	/** 确认银行账号 */
	private String comfirmAccount;
	/** 交易密码 */
	private String payPwd;
	/** 开始时间 **/
	private String startTime;
	/** 结束时间 **/
	private String endTime;
	/** 开始时间 **/
	private Date startDate;
	/** 结束时间 **/
	private Date endDate;
	
	/** 提现状态名 **/
    private String statusStr;
    
    /** 取现返回码 **/
    private String returnCode;
    
    /**取现服务费**/
    private double servFee;
    
    /**
     * 提现时间值
     */
    private String addTimeStr;
    /**日期范围：0：全部，1：最近七天 2：最近一个月  3：最近两个月，4 最近三个月**/
    private int time;
    /** 条件查询 **/
    private String searchName;
	/**
	 * 
	 * @param accountCash
	 * @return
	 */
	public static AccountCashModel instance(AccountCash accountCash) {
		AccountCashModel accountCashModel = new AccountCashModel();
		BeanUtils.copyProperties(accountCash, accountCashModel);
		return accountCashModel;
	}

	/**
	 * @param bank
	 * @return
	 */
	public AccountCash prototype(AccountBank bank,String bankName) {
		User user = UserUtils.getUserById(bank.getUserId());
		AccountCash accountCash = new AccountCash(user, getMoney());
		accountCash.setBankNo(bank.getBankNo());
		accountCash.setBank(bankName);
		accountCash.setBranch(bank.getBranch());
		accountCash.setAddTime(DateUtil.getNow());
		accountCash.setAddIp(Global.getIP());
		return accountCash;
	}
	
	public void setStartDateAndEndDate(){
		if(StringUtils.isNotBlank(startTime)){
			startDate = DateUtil.valueOf(startTime + " 00:00:00");
		}
		if(StringUtils.isNotBlank(endTime)){
			endDate = DateUtil.valueOf(endTime + " 23:59:59");
		}
	}
	
	/**
	 * App解绑银行卡参数校验
	 * @return
	 */
	public Map<String,Object> validBankNo(){
		if(StringUtil.isBlank(getBankNo())){
			Map<String,Object> data = new HashMap<String,Object>();
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppCash03"));
			return data;
		}
		return null;
	}
	/**
	 * 添加银行卡校验
	 * @param countTotal 银行卡总数
	 * @param defaultTotal 系统设置的可添加银行卡最大数
	 * @param bank 个人银行账户实体类
	 */
	public void validAddBank(int countTotal, int defaultTotal, AccountBank bank) {
		if (countTotal >= defaultTotal) {
			throw new AccountException("添加银行不能超过设置的最大数" + defaultTotal + "个！", 1);
		}
		if (StringUtil.isBlank(bank.getBankNo())) {
			throw new AccountException("银行账号不能为空！", 1);
		}
		if (!bank.getBankNo().equals(getComfirmAccount())) {
			throw new AccountException("银行账号和确认银行账号不一致！", 1);
		}
		if (StringUtil.isBlank(bank.getBank())) {
			throw new AccountException("银行名称不能为空！", 1);
		}
	}

	/**
	 * 客服提现审核
	 * @param accountCash 资金提现记录实体类
	 */
	public void validKFVerify(AccountCash accountCash) {
		if (accountCash == null || accountCash.getStatus() != 0) {
			throw new AccountException("提现审核失败！", 1);
		}
	}
	
	/**
	 * 财务提现审核
	 * @param accountCash 资金提现记录实体类
	 */
	public void validCWVerify(AccountCash accountCash) {
		if (accountCash == null || accountCash.getStatus() != 5) {
			throw new AccountException("提现审核失败！", 1);
		}
	}
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public long getBankId() {
		return bankId;
	}

	public void setBankId(long bankId) {
		this.bankId = bankId;
	}

	public String getComfirmAccount() {
		return comfirmAccount;
	}

	public void setComfirmAccount(String comfirmAccount) {
		this.comfirmAccount = comfirmAccount;
	}

	public String getPayPwd() {
		return payPwd;
	}

	public void setPayPwd(String payPwd) {
		this.payPwd = payPwd;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	/**
     * 设置状态 状态 0：申请提现  1：提现成功，2：提现失败 4：用户取消提现 ，
     * 
     */
	public String getStatusStr() {
        switch (getStatus()) {
            case 0:
                statusStr="提现申请中";
                break;
            case 1:
                statusStr="提现成功";
                break;
            case 2:
                statusStr="提现失败";
                break;
            case 4:
                statusStr="提现申请中";// 新浪用
                break;                
            default:
                statusStr="提现异常";
                break;
        }
        return statusStr;
    }
	
    public void setStatusStr(String statusStr) {
        this.statusStr = statusStr;
    }

    public String getAddTimeStr() {
    	return DateUtil.dateStr4(getAddTime());
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

    public double getServFee() {
		return servFee;
	}

	public void setServFee(double servFee) {
		this.servFee = servFee;
	}

	public String getReturnCode() {
		return returnCode;
	}

	public void setReturnCode(String returnCode) {
		this.returnCode = returnCode;
	}

	/**
     * 提现总额转换为字符串防止数值过大变成科学计数法显示
     * @return
     */
    public String getMoneyString() {
    	return new BigDecimal(Double.toString(getMoney())).toPlainString();
    }
    /**
     * 到账金额转换为字符串防止数值过大变成科学计数法显示
     * @return
     */
    public String getAmountString() {
    	return new BigDecimal(Double.toString(getAmount())).toPlainString();
    }
    
    public String getFeeBearTip(){
    	if(getFeeBear() == 1){
    		return "平台垫付";
    	} else if(getFeeBear() == 2){
    		return "个人支付";
    	}
    	return "";
    }
}
